<?php
function GetApi($url, $data)
{
    $data['sign'] = sign($data);
    return http_post($url, $data);
}
function sign($map)
{
    $rList = [];
    foreach ($map as $k => $v) {
        if ($k == "other_settle_params" || $k == "app_id" || $k == "sign" || $k == "thirdparty_id")
            continue;
        $value = trim(strval($v));
        if (is_array($v)) {
            $value = arrayToStr($v);
        }
        $len = strlen($value);
        if ($len > 1 && substr($value, 0, 1) == "\"" && substr($value, $len - 1) == "\"")
            $value = substr($value, 1, $len - 1);
        $value = trim($value);
        if ($value == "" || $value == "null")
            continue;
        $rList[] = $value;
    }
    $rList[] = "Sw0vPepn8dUUqHgEuZUdAkHaCWGnPD4HMMH9qMXl";
    sort($rList, SORT_STRING);
    return md5(implode('&', $rList));
}

function arrayToStr($map)
{
    $isMap = isArrMap($map);
    $result = "";
    if ($isMap) {
        $result = "map[";
    }
    $keyArr = array_keys($map);
    if ($isMap) {
        sort($keyArr);
    }
    $paramsArr = array();
    foreach ($keyArr as $k) {
        $v = $map[$k];
        if ($isMap) {
            if (is_array($v)) {
                $paramsArr[] = sprintf("%s:%s", $k, arrayToStr($v));
            } else {
                $paramsArr[] = sprintf("%s:%s", $k, trim(strval($v)));
            }
        } else {
            if (is_array($v)) {
                $paramsArr[] = arrayToStr($v);
            } else {
                $paramsArr[] = trim(strval($v));
            }
        }
    }
    $result = sprintf("%s%s", $result, join(" ", $paramsArr));
    if (!$isMap) {
        $result = sprintf("[%s]", $result);
    } else {
        $result = sprintf("%s]", $result);
    }
    return $result;
}

function isArrMap($map)
{
    foreach ($map as $k => $v) {
        if (is_string($k)) {
            return true;
        }
    }

    return false;
}

function http_post($url, $data)
{
    $postdata = json_encode($data);

    $options = array(
        'http' => array(
            'method' => 'POST',
            'header' => 'Content-type:application/json',
            'content' => $postdata,
            'timeout' => 15 * 60
        )
    );
    $context = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    return $result;
}